package com.master.controller.householder;

import com.master.common.utils.EmptyUtil;
import com.master.common.utils.Result;
import com.master.service.householder.HouseholdService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Heath.Que
 * @version 1.0
 * @classname
 * @description 住户信息
 * @Date 2021/3/24 16:34
 **/
@RestController
@RequestMapping(value = "/neighbourhood/householder")
public class CsHouseholdController {

    @Autowired
    private HouseholdService householdService;

    /**
     * @Description: 按条件查询住户记录
     * @Parameter: [params]
     * @Return: com.master.common.utils.Result
     * @author: Heath.Que
     * @Date: 2021/5/28
     **/
    @PostMapping(value = "/getHouseholders")
    public Result getHouseholderByCondition(@RequestBody CsHouseholderVO csHouseholderVO) {
        Map<String, Object> searcherCondition = csHouseholderVO.getSearchCondition();
        //按条件查询
        List<Map<String, Object>> householders = householdService.getHouseholdersByCondition(searcherCondition);

        return Result.success(householders);
    }

    @PostMapping(value = "/addHouseholder")
    public Result addHouseholder(@ApiParam(name = "csHouseholderVO", value = "房屋编号和住户信息集合") @RequestBody CsHouseholderVO csHouseholderVO) {
        if (EmptyUtil.isEmpty(csHouseholderVO.getHouseholders())) {
            return Result.fail("传入参数为空，请重新操作");
        }

        boolean mark;
        if (EmptyUtil.isEmpty(csHouseholderVO.getHouseId())) {//普通数据添加
            mark = householdService.addHouseholders(csHouseholderVO);
        } else {//按门牌号添加
            mark = householdService.addHouseholdersByHouseId(csHouseholderVO);
        }

        if (mark) {
            return Result.success("操作成功");
        } else {
            return Result.fail("操作失败");
        }
    }

    @PostMapping(value = "/modifyHouseholder")
    public Result modifyHouseholder(@ApiParam(name = "csHouseholderVO", value = "修改数据对象") @RequestBody CsHouseholderVO csHouseholderVO) {
        if (EmptyUtil.isNotEmpty(csHouseholderVO.getHouseholders())) {
            return Result.fail("传入参数为空，请重新操作");
        }
        boolean mark = householdService.modifyHouseholders(csHouseholderVO);
        if (mark) {
            return Result.success("操作成功");
        } else {
            return Result.fail("操作失败");
        }
    }


    @PostMapping(value = "/deleteHouseholder")
    public Result deleteHouseholder(@RequestBody List<Long> householderIds) {
        if (EmptyUtil.isEmpty(householderIds)) {
            return Result.fail("传入参数为空，请重新操作");
        }
        boolean mark = householdService.deleteHouseholders(householderIds);
        if (mark) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

}
